package com.threeox.commonlibrary.ui.view.navigationview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.ui.view.inter.engine.listmodel.INavigationView;
import com.threeox.commonlibrary.ui.view.inter.engine.listmodel.OnTouchNavigationChangedListener;
import com.threeox.utillibrary.util.ConvertUtils;
import com.threeox.utillibrary.util.EmptyUtils;

/**
 * 
 * @ClassName: CommonNavigationView
 * 
 * @Description: TODO()
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间: 2017/8/9 10:41
 * 
 * @version 1.0
 * 
 */
public class CommonNavigationView extends View implements INavigationView {

    // 数组
    private String[] mCharArray = new String[]{};

    private int mChoose = -1;// 选中
    private Paint mPaint = new Paint();

    private TextView mDialogView;
    private OnTouchNavigationChangedListener mOnTouchNavigationChangedListener;

    public CommonNavigationView(Context context) {
        this(context, null);
    }

    public CommonNavigationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 重写这个方法
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (EmptyUtils.isNotEmpty(mCharArray)) {
            // 获取焦点改变背景颜色.
            int height = getHeight();// 获取对应高度
            int width = getWidth(); // 获取对应宽度
            int singleHeight = height / mCharArray.length;// 获取每一个字母的高度

            for (int i = 0; i < mCharArray.length; i++) {
                mPaint.setColor(getResources().getColor(R.color.color_bottom_text_normal));
                mPaint.setTypeface(Typeface.DEFAULT_BOLD);
                mPaint.setAntiAlias(true);
                mPaint.setTextSize(ConvertUtils.sp2px(getContext(), 12));
                // 选中的状态
                if (i == mChoose) {
                    mPaint.setColor(Color.parseColor("#3399ff"));
                    mPaint.setFakeBoldText(true);
                }
                // x坐标等于中间-字符串宽度的一半.
                float xPos = width / 2 - mPaint.measureText(mCharArray[i]) / 2;
                float yPos = singleHeight * i + singleHeight;
                canvas.drawText(mCharArray[i], xPos, yPos, mPaint);
                mPaint.reset();// 重置画笔
            }
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();// 点击y坐标
        final int oldChoose = mChoose;
        final int c = (int) (y / getHeight() * mCharArray.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
        switch (action) {
            case MotionEvent.ACTION_UP:
                setBackgroundDrawable(new ColorDrawable(0x00000000));
                mChoose = -1;//
                invalidate();
                if (mDialogView != null) {
                    mDialogView.setVisibility(View.INVISIBLE);
                }
                break;

            default:
                // 设置右侧字母列表[A,B,C,D,E....]的背景颜色
                setBackgroundResource(R.drawable.navigationview_background);
                if (oldChoose != c) {
                    if (c >= 0 && c < mCharArray.length) {
                        if (mOnTouchNavigationChangedListener != null) {
                            mOnTouchNavigationChangedListener.onTouchNavigationChanged(mCharArray[c]);
                        }
                        if (mDialogView != null) {
                            mDialogView.setText(mCharArray[c]);
                            mDialogView.setVisibility(View.VISIBLE);
                        }
                        mChoose = c;
                        invalidate();
                    }
                }
                break;
        }
        return true;
    }

    @Override
    public void setCharArray(String[] charArray) {
        try {
            this.mCharArray = charArray;
            postInvalidate();
        } catch (Exception e) {

        }
    }

    @Override
    public void setDialogView(TextView dialogView) {
        this.mDialogView = dialogView;
    }

    @Override
    public void setOnTouchNavigationChangedListener(OnTouchNavigationChangedListener onTouchNavigationChangedListener) {
        this.mOnTouchNavigationChangedListener = onTouchNavigationChangedListener;
    }
}
